<?php

namespace App\Admin\Controllers\Aliyun;

use App\Admin\Actions\Aliyun\MakeMiddleDomain;
use App\Admin\Actions\Aliyun\MakePlayerDomain;
use App\Admin\Actions\Aliyun\MakeRankDomain;
use App\Admin\Actions\Aliyun\MakeStatementDomain;
use App\Admin\Actions\Aliyun\MakeStorageDomain;
use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Renders\PoolDomainUsed;
use App\Enums\DomainSource;
use App\Enums\DomainType;
use App\Models\Aliyun;
use App\Models\DomainPool;
use Dcat\Admin\Admin;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class PoolController extends AdminController
{
    protected string $title = '域名池';

    /**
     * @throws InvalidArgumentException
     */
    public function grid()
    {
        $model = DomainPool::ofAdmin()
            ->withCount(['spiders'])
            ->latest();

        return Grid::make($model, function (Grid $grid) {
            $grid->filter(function (Grid\Filter $f) {
                $f->like('domain', '域名地址');
            });

            $grid->tools([
                new MakeMiddleDomain,
                new MakePlayerDomain,
                new MakeRankDomain,
                new MakeStatementDomain,
                new MakeStorageDomain,
            ]);

            $grid->disableBatchDelete(false);
            $grid->disableCreateButton();

            $grid->batchActions([
                new BatchEnable(DomainPool::class),
                new BatchDisable(DomainPool::class),
            ]);

            $grid->disableEditButton();

            $grid->quickSearch(['domain'])
                ->placeholder('域名地址');

            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }
            $grid->column('source', '域名来源')
                ->using(DomainSource::SOURCE_MAP);
            $grid->column('domain', '域名地址')
                ->copyable();
            $grid->column('type', '域名类型')
                ->using(DomainType::TYPE_MAP)
                ->label(DomainType::TYPE_LABEL);
            $grid->column('status')
                ->bool();
            $grid->column('ssl', '开启SSL')
                ->bool();
            $grid->column('spiders_count', '应用点')
                ->modal(function ($modal) {
                    $modal->title($this->domain);

                    return PoolDomainUsed::make();
                });
            $grid->column('remark', '备注')
                ->textarea();
            $grid->column('created_at');
        });
    }

    public function domains(Request $request): array
    {
        $aliyunId = $request->get('q');
        $aliyun = Aliyun::ofAdmin()->find($aliyunId);

        return $aliyun->getDomains();
    }

    /**
     * @throws InvalidArgumentException
     */
    public function form()
    {
        return Form::make(DomainPool::ofAdmin(), function (Form $form) {
            $form->textarea('remark', '备注');
        });
    }
}
